package com.ds.controller.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.HtmlUtils;

import com.ds.bo.BlogGroupBO;
import com.ds.core.base.BaseController;
import com.ds.core.util.DataUtil;
import com.ds.core.util.MarkUtil;
import com.ds.orm.model.Blog;
import com.ds.orm.model.BlogClassify;
import com.ds.orm.model.User;
import com.ds.orm.model.WebsiteConf;
import com.ds.service.BlogClassifyService;
import com.ds.service.BlogService;
import com.ds.service.UserService;
import com.ds.service.WebsiteConfService;

@Controller
@RequestMapping("v/blog")
public class BlogView{

	protected final Logger logger = LogManager.getLogger(this.getClass());
	@Autowired
	private BlogService blogService;
	@Autowired
	private UserService userService;
	@Autowired
	private BlogClassifyService blogClassifyService;
	@Autowired
	private WebsiteConfService websiteConfService;
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ModelAndView blog(@PathVariable("id")long id,ModelMap modelMap) {
		Blog blog=blogService.queryById(id);
		if (blog==null||!"0".equals(blog.getDelFlag())) {
			return new ModelAndView("common/404");
		}
		ModelAndView mv=new ModelAndView("blog");
		long authorId=blog.getAuthorId();
		List<BlogGroupBO> monthGroup= blogService.getMonthGroup(authorId);
		User author=userService.queryById(authorId);
		//logger.debug(blog.toString());
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userId", authorId);
		map.put("delFlag", 0);
		List<BlogClassify> blogClassifys=blogClassifyService.queryList(map);
		mv.addObject("blog", blog);
		mv.addObject("monthGroup", monthGroup);
		mv.addObject("author", author);
		mv.addObject("blogClassifys", blogClassifys);
		return mv;
	}
	
	@RequestMapping(value = "{url}/list", method = RequestMethod.GET)
	public ModelAndView bloglist(@PathVariable("url")String url,ModelMap modelMap) {
		if (DataUtil.isNotEmpty(modelMap.get("orderBy"))) {
			modelMap.put("orderBy","create_time");
		}
		WebsiteConf conf=websiteConfService.queryByUrl(url);
		long authorId=conf.getUserId();
		User author=userService.queryById(authorId);
		List<BlogGroupBO> monthGroup= blogService.getMonthGroup(authorId);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userId", authorId);
		map.put("delFlag", 0);
		List<BlogClassify> blogClassifys=blogClassifyService.queryList(map);
		modelMap.put("authorId", authorId);
		modelMap.put("delFlag", 0);
		List<Blog> blogs=blogService.queryPageList(modelMap);
		ModelAndView mv=new ModelAndView("bloglist");
		mv.addObject("blogs", blogs);
		mv.addObject("monthGroup", monthGroup);
		mv.addObject("author", author);
		mv.addObject("blogClassifys", blogClassifys);
		return mv;
	}
	
	
}
